	
	package Dao.daoimpl;
	
	import java.sql.Connection;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.ArrayList;
	import java.util.List;
	import unitls.DataBaseConnection;
	import Dao.AdminDao;
	import enitys.Admin;

	
	public class AdminDaoImpl implements AdminDao {
		private  Connection conn = null;
		private Statement sta = null;
		private  ResultSet rs = null;
		String sql = null;
	
		public boolean addAdmin(Admin admin) throws Exception {
			// TODO Auto-generated method stub
			boolean flag = false;
			this.sql = "select * form admin where name="+admin.getName();// 添加时先判断管理员的名字是否已经存在
			this.conn = DataBaseConnection.getConnection();
			try {
					this.sta = this.conn.createStatement();
					this.rs = this.sta.executeQuery(this.sql);
					if (rs.next()) {
						int a_id = rs.getInt("a_id");
						// 查询是否已经添加过
						this.sql = "select * from admin where a_id = '" + a_id;
						this.rs = this.sta.executeQuery(this.sql);
					if (!rs.next()) {
						this.sql="insert into admin(name,pass,email) values('" +admin.getName() + "','" + admin.getPass()+ "',"
									+ "'" + admin.getEmail() + "')";
							this.sta.executeUpdate(this.sql);
							return true;
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
				// 关闭连接
					DataBaseConnection.closeRs(this.rs);
					DataBaseConnection.closeStmt(this.sta);
					DataBaseConnection.closeConn(this.conn);
				}

			
			return flag;
		}
	
		public List<Admin> findAdmin() throws Exception {
			// TODO Auto-generated method stub
			List<Admin> list = new ArrayList<Admin>(); // 用List集合来保存管理员的信息
			sql = "select * from admin";
			this.conn = DataBaseConnection.getConnection();
			try {
				this.sta = this.conn.createStatement();
				this.rs = this.sta.executeQuery(this.sql);
				// 把信息放进List
				while (this.rs.next()) {
					Admin temp = new Admin();
					temp.setA_id(this.rs.getInt("a_id"));
					temp.setName(this.rs.getString("name"));
					temp.setPass(this.rs.getString("pass"));
					temp.setEmail(this.rs.getString("email"));
					list.add(temp);
				}
				return list;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			//若失败返回null
			return null;
		}
		@Override
		public List<Admin> findAdminById(int id) throws Exception {
			// TODO Auto-generated method stub
			this.sql="select * from admin where a_id="+id;
			this.conn=DataBaseConnection.getConnection();
			this.sta=this.conn.createStatement();
			rs=this.sta.executeQuery(sql);
			List<Admin> list=new ArrayList<Admin>();
			while(rs.next()){
				Admin admin=new Admin();
				admin.setA_id(rs.getInt("a_id"));
				admin.setName(rs.getString("name"));
				admin.setPass(rs.getString("pass"));
				admin.setEmail(rs.getString("email"));
				list.add(admin);
			}
			return list;
		}
			
		public boolean modifadmin(Admin admin) throws Exception {
			// TODO Auto-generated method stub
			int rs=0;
			this.sql="update admin set name="+admin.getName()+"and pass="+admin.getPass()+"and email="+admin.getEmail()+"where a_id="+admin.getA_id();
			this.conn=DataBaseConnection.getConnection();
			try {
				sta=this.conn.createStatement();
				rs=sta.executeUpdate(this.sql);
			if(rs>0){
				return true;
			}
			}catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			return false;
		}
	
		public int deleteadmin(Admin admin) throws Exception {
			// TODO Auto-generated method stub
			int rs=0;
			sql="delete from admin where a_id="+admin.getA_id();
			this.conn = DataBaseConnection.getConnection();
			try {
				this.sta = this.conn.createStatement();
				rs = this.sta.executeUpdate(this.sql);
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.sta);
				DataBaseConnection.closeConn(this.conn);
			}
			return rs;
		}

		
	
	}
